import $ from 'jquery';

export function createFormDefinition() {
    return {
        id: undefined,
        formName: '新建表单',
        fields: [],
        subForms: [],
        views: [createView('主视图', true)],
        actions: [],
        fieldFormulas: [],
    }
}

export function createView(viewName, main = false) {
    return {
        id: undefined,
        viewName: viewName,
        main: main,
        html: undefined,
        configs: undefined,//字符串, 用来做vue的渲染
    };
}


export function createField() {
    return {
        id: undefined,
        fieldName: undefined,
        fieldTitleName: undefined,
        label: undefined,
        component: undefined, //组件名称
        configs: undefined, //字符串, 存储的是字段的属性, 里面是一个json
    }
}

export function parseFormDefinitionViews(formDefinition) {
    (formDefinition.views || []).forEach((view) => {
        parseFormDefinitionView(view)
    })
}

export const dataViewTypes = {
    field: 'field'
}

export function parseFormDefinitionView(view) {
    let res = [], list = $(view.html);
    for (let i = 0; i < list.length; i++) {
        let item = parseFormDefinitionViewItem(list[i]);
        item && res.push(item);
    }
    view.configs = res;
}

export function parseFormDefinitionViewItem(ele) {
    let dataViewType = $(ele).attr('data-view-type');
    let id = $(ele).attr('data-id');

    if (dataViewType === dataViewTypes.field) {
        return {dataViewType, id};
    }
}